$(function () {
    var mainNav = $('#mainNav');
    var subNav = $('#subNav');

    $(window).resize(function () {
        var winH = $(window).height();
        var headerH = $("#header").outerHeight(true);
        $("#body").css("height", winH - headerH);
        $('#mainContainer').height($('#body').height() - 10);
        niceScrollbar($('#leftBar'));
    }).resize();

    // 头部userbar链接点击
    $('.header-menu').find('a').each(function () {
        var linkTarget = $(this).attr('target');
        if (linkTarget === 'contentPage') {
            $(this).click(function () {
                $('#welcome').hide();
                $('#mainContainer').show();
            });
        }
    });

    // 边栏导航折叠
    $("#splitBar").click(function () {
        var hasClsOff = $(this).hasClass('split-bar-off');
        if (!hasClsOff) {
            $('#sidebar').animate({"width": 0}, function () {
                $("#subNav").hide();
                $('#splitBar').addClass("split-bar-off");
            });
        } else {
            $("#subNav").show();
            $('#sidebar').animate({"width": 156});
            $(this).removeClass("split-bar-off");
        }
    });

    // 二级导航切换到一级导航
    $('#subNavBtn').click(function () {
        var clickTarget = $(this);
        mainNav.slideDown(function () {
            docClickHide(clickTarget, mainNav); //点击任意地方收起一级导航

            var mainIframeDoc = document.getElementById('contentPage').contentDocument.body;
            $(mainIframeDoc).click(function () {
                mainNav.slideUp();
            });
        });
    });

    //点击任意地方隐藏右键菜单
//    $(document).click(function() { $('#contextMenu').hide();});
    $('#contextMenu').hover(function () {
//        $(this).show();
    }, function () {
        $(this).hide();
    });

    //呼叫中心打开
    $('#callCenter').click(function () {
        $('.call-center').animate({top: 0}, 100);
    });

    //呼叫中心打开
    $('#callClose').click(function () {
        $('.call-center').animate({top: -75}, 100);
    });

    //右下角消息弹出框
    var popMsgText = '';
//    $.messager.show('云ERP推送消息', popMsgText, 0);
});

//创建二级导航及绑定事件
function createSubNav(data, wrapObj, pid) {
    //检测请求的dom是否存在，如果不存在就创建
    data = eval(data);
    if (!wrapObj.length) {
        var accordionStr = '<div id="sub-' + pid + '">';
        for (var j = 0; j < data.length; j++) {
            if (data[j].children.length > 0) {
                accordionStr += '<div class="accordion-panel">';
                accordionStr += '<div class="accordion-hd">';
                accordionStr += '<div class="accordion-title">' + data[j].accMenuName + '</div>';
                accordionStr += '<a class="accordion-collapse" href="javascript:;"></a>';
                accordionStr += '</div><ul class="accordion-bd">';
                //生成三级导航
                for (var k = 0; k < data[j].children.length; k++) {
                    accordionStr += '<li>';
                    accordionStr += '<i class="list-icon"></i>';
                    accordionStr += '<a href="' + data[j].children[k].accMenuUrl + '" target="contentPage">' + data[j].children[k].accMenuName + '</a>';
                    accordionStr += '</li>';
                }
                accordionStr += '</ul></div>';
            }
        }
        accordionStr += "</div>";
        wrapObj = $(accordionStr).appendTo($('#leftBar'));
        $(wrapObj).show().siblings().hide();

        //展开收缩
        wrapObj.find('.accordion-hd').on('click', function () {
            var acrBd = $(this).next('.accordion-bd');
            if (acrBd.is(':visible')) {
                acrBd.slideUp();
            } else {
                acrBd.slideDown();
                $(this).parent('.accordion-panel').siblings().find('.accordion-bd').slideUp();
            }
        });
    }
    $(wrapObj).show().siblings().hide();
    //每个accordion-panel里最后一个链接添加 .last
    wrapObj.find('.accordion-panel').each(function () {
        $(this).find('.accordion-bd').find('li').eq(-1).addClass('last');
    });

    //点击三级菜单链接时添加高亮
    wrapObj.find('.accordion-bd').find("li").click(function (e) {
        if (e.which == 1) {
            $(this).addClass('current').siblings().removeClass('current');
            $(this).closest('.accordion-panel').siblings().find('li').removeClass('current');
            $(this).closest('.accordion-panel').parent().siblings().find('li').removeClass('current');
            $(this).closest('.accordion-panel').parent().siblings().find('.accordion-bd').hide();
            $('#mainContainer, #splitBar').show();
            $('#welcome').hide();
        }
    });

    //绑定右键菜单
    wrapObj.find('.accordion-bd').find("li").on('contextmenu', function (e) {
        var pIndex1 = $(this).closest('.accordion-panel').index();
        var pIndex2 = $(this).index();
        var href = $(this).find('a').attr('href');
        //显示并定位右键菜单
        var x = $(this).offset().left;
        var y = $(this).offset().top + $(this).height();
        $('#contextMenu').css({"display": "block", "top": y, "left": x});
        var host = "http://" + window.location.host + window.location.pathname;
        var url = host + '?menu_mark=' + pid + '/' + pIndex1 + '/' + pIndex2;

        //右键功能新标签url
        $('#newPageOpen').find('a').attr('href', url);

        //右键功能 - 打开
        var openUrl = $(this).find('a').attr('href');
        $('#pageOpen').find('a').attr('href', openUrl);
        $('#mainNav').hide();
        $('#subNav').show();
        return false;
    });
}

//同步一级导航的icon和文字
function navTitle(obj) {
    var iconStr = obj.find('.icon-box').html();
    var textStr = obj.text();
    $('#subTitle').html(iconStr + textStr);
}

/**
 * 点击文档任意地方，收起指定容器
 * @clickObj [object] 需要点击的DOM对象
 * @obj [object] 弹出的DOM对象
 */
function docClickHide(clickObj, obj) {
    $(document).on('click', function (e) {
        var isTarget = $(e.target).closest(clickObj).length;
        if (isTarget) {
            $(obj).slideDown();
        } else {
            $(obj).slideUp();
        }
    });
}

// 滚动条美化
function niceScrollbar(obj) {
    obj.niceScroll({
        touchbehavior: false,
        autohidemode: "scroll",
        cursorcolor: "#fff",
        cursorborder: 0,
        cursoropacitymin: 0.3,
        cursoropacitymax: 0.7,
        cursorwidth: 4
    });
}

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return (r[2]);
    return null;
}